<template>
  <div class="hexagrams-container">
    <div class="header">
      <h1>六十四卦</h1>
      <p class="subtitle">探索易经的智慧 - 完整64卦系统</p>
      <div class="stats">
        <span class="stat-item">上经30卦</span>
        <span class="stat-item">下经34卦</span>
        <span class="stat-item">总计64卦</span>
      </div>
    </div>

    <div class="search-section">
      <input 
        v-model="searchTerm" 
        placeholder="搜索卦名或描述..."
        class="search-input"
      />
      <div class="filter-buttons">
        <button 
          @click="filterType = 'all'" 
          :class="{ active: filterType === 'all' }"
          class="filter-btn"
        >
          全部
        </button>
        <button 
          @click="filterType = 'upper'" 
          :class="{ active: filterType === 'upper' }"
          class="filter-btn"
        >
          上经 (1-30)
        </button>
        <button 
          @click="filterType = 'lower'" 
          :class="{ active: filterType === 'lower' }"
          class="filter-btn"
        >
          下经 (31-64)
        </button>
      </div>
    </div>

    <div class="hexagrams-grid">
      <div 
        v-for="hexagram in filteredHexagrams" 
        :key="hexagram.id"
        class="hexagram-card"
        :class="{ 'upper-classic': hexagram.id <= 30, 'lower-classic': hexagram.id > 30 }"
        @click="selectHexagram(hexagram)"
      >
        <div class="hexagram-header">
          <span class="hexagram-number">第{{ hexagram.id }}卦</span>
          <span class="classic-type">{{ hexagram.id <= 30 ? '上经' : '下经' }}</span>
        </div>
        <div class="hexagram-symbol">
          <div class="hexagram-lines">
            <div v-for="(line, index) in getHexagramLines(hexagram.symbol)" :key="index" class="hexagram-line">
              {{ line }}
            </div>
          </div>
        </div>
        <div class="hexagram-info">
          <h3>{{ hexagram.chineseName }}</h3>
          <p class="english-name">{{ hexagram.name }}</p>
          <p class="description">{{ hexagram.description }}</p>
          <p class="interpretation">{{ hexagram.interpretation }}</p>
        </div>
      </div>
    </div>

    <div v-if="selectedHexagram" class="modal-overlay" @click="closeModal">
      <div class="modal-content" @click.stop>
        <div class="modal-header">
          <h2>{{ selectedHexagram.chineseName }} - {{ selectedHexagram.name }}</h2>
          <button @click="closeModal" class="close-btn">×</button>
        </div>
        <div class="modal-body">
          <div class="hexagram-display">
            <div class="symbol-large">
              <div class="hexagram-lines-large">
                <div v-for="(line, index) in getHexagramLines(selectedHexagram.symbol)" :key="index" class="hexagram-line-large">
                  {{ line }}
                </div>
              </div>
            </div>
            <div class="info">
              <p><strong>象征：</strong>{{ selectedHexagram.description }}</p>
              <p><strong>卦辞：</strong>{{ selectedHexagram.interpretation }}</p>
            </div>
          </div>
          <div class="lines-section">
            <h3>爻辞</h3>
            <div class="lines">
              <div v-for="(line, index) in selectedHexagram.lines" :key="index" class="line">
                {{ line }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'

const hexagrams = ref([])
const searchTerm = ref('')
const filterType = ref('all')
const selectedHexagram = ref(null)

const filteredHexagrams = computed(() => {
  let filtered = hexagrams.value

  // 按类型筛选
  if (filterType.value === 'upper') {
    filtered = filtered.filter(hexagram => hexagram.id <= 30)
  } else if (filterType.value === 'lower') {
    filtered = filtered.filter(hexagram => hexagram.id > 30)
  }

  // 按搜索词筛选
  if (searchTerm.value) {
    const term = searchTerm.value.toLowerCase()
    filtered = filtered.filter(hexagram => 
      hexagram.chineseName.toLowerCase().includes(term) ||
      hexagram.name.toLowerCase().includes(term) ||
      hexagram.description.toLowerCase().includes(term) ||
      hexagram.interpretation.toLowerCase().includes(term) ||
      hexagram.id.toString().includes(term)
    )
  }

  return filtered
})

const selectHexagram = (hexagram) => {
  selectedHexagram.value = hexagram
}

const closeModal = () => {
  selectedHexagram.value = null
}

const getHexagramLines = (symbol) => {
  // 将卦象符号转换为6爻显示
  // symbol格式如 "☰☰" 表示两个三爻卦
  const lines = []
  
  // 解析上卦和下卦
  const upperTrigram = symbol[0]
  const lowerTrigram = symbol[1]
  
  // 八卦对应的三爻
  const trigramLines = {
    '☰': ['━━━', '━━━', '━━━'], // 乾
    '☷': ['━ ━', '━ ━', '━ ━'], // 坤
    '☳': ['━━━', '━ ━', '━ ━'], // 震
    '☴': ['━ ━', '━━━', '━━━'], // 巽
    '☵': ['━ ━', '━━━', '━ ━'], // 坎
    '☲': ['━━━', '━ ━', '━━━'], // 离
    '☶': ['━ ━', '━ ━', '━━━'], // 艮
    '☱': ['━━━', '━━━', '━ ━']  // 兑
  }
  
  // 组合成六爻（上卦在上，下卦在下）
  const upperLines = trigramLines[upperTrigram] || ['━ ━', '━ ━', '━ ━']
  const lowerLines = trigramLines[lowerTrigram] || ['━ ━', '━ ━', '━ ━']
  
  // 从上到下排列（上九、九五、九四、九三、九二、初九）
  lines.push(upperLines[2]) // 上九
  lines.push(upperLines[1]) // 九五
  lines.push(upperLines[0]) // 九四
  lines.push(lowerLines[2]) // 九三
  lines.push(lowerLines[1]) // 九二
  lines.push(lowerLines[0]) // 初九
  
  return lines
}

const fetchHexagrams = async () => {
  try {
    const response = await fetch('http://localhost:3000/yijing/hexagrams')
    hexagrams.value = await response.json()
  } catch (error) {
    console.error('获取卦象失败:', error)
  }
}

onMounted(() => {
  fetchHexagrams()
})
</script>

<style scoped>
.hexagrams-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  font-family: 'Microsoft YaHei', sans-serif;
}

.header {
  text-align: center;
  margin-bottom: 40px;
}

.header h1 {
  font-size: 2.5rem;
  color: #2c3e50;
  margin-bottom: 10px;
}

.subtitle {
  font-size: 1.1rem;
  color: #7f8c8d;
  font-style: italic;
}

.stats {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-top: 15px;
}

.stat-item {
  background: rgba(255,255,255,0.2);
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 0.9rem;
  color: white;
}

.search-section {
  margin-bottom: 30px;
}

.search-input {
  width: 100%;
  max-width: 400px;
  padding: 12px 20px;
  border: 2px solid #bdc3c7;
  border-radius: 25px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.search-input:focus {
  outline: none;
  border-color: #3498db;
}

.filter-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 20px;
  flex-wrap: wrap;
}

.filter-btn {
  padding: 8px 16px;
  border: 2px solid #bdc3c7;
  background: white;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.3s;
  font-size: 0.9rem;
}

.filter-btn:hover {
  border-color: #3498db;
  color: #3498db;
}

.filter-btn.active {
  background: #3498db;
  color: white;
  border-color: #3498db;
}

.hexagrams-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.hexagram-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.1);
  cursor: pointer;
  transition: all 0.3s;
  border: 2px solid transparent;
}

.hexagram-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0,0,0,0.15);
  border-color: #3498db;
}

.hexagram-card.upper-classic {
  border-left: 4px solid #e74c3c;
}

.hexagram-card.lower-classic {
  border-left: 4px solid #27ae60;
}

.hexagram-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #ecf0f1;
}

.hexagram-number {
  font-size: 0.8rem;
  color: #7f8c8d;
  font-weight: bold;
}

.classic-type {
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 10px;
  color: white;
  font-weight: bold;
}

.upper-classic .classic-type {
  background: #e74c3c;
}

.lower-classic .classic-type {
  background: #27ae60;
}

.hexagram-symbol {
  text-align: center;
  margin-bottom: 15px;
}

.hexagram-lines {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.hexagram-line {
  font-family: 'Courier New', monospace;
  font-size: 1.2rem;
  color: #2c3e50;
  line-height: 1;
  font-weight: bold;
}

.hexagram-info h3 {
  color: #2c3e50;
  margin-bottom: 8px;
  font-size: 1.5rem;
}

.english-name {
  color: #7f8c8d;
  font-size: 1rem;
  margin-bottom: 10px;
  font-style: italic;
}

.description {
  color: #34495e;
  font-size: 1.1rem;
  margin-bottom: 8px;
}

.interpretation {
  color: #27ae60;
  font-weight: bold;
  font-size: 1rem;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-content {
  background: white;
  border-radius: 12px;
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20px;
  border-bottom: 1px solid #ecf0f1;
}

.modal-header h2 {
  color: #2c3e50;
  margin: 0;
}

.close-btn {
  background: none;
  border: none;
  font-size: 2rem;
  cursor: pointer;
  color: #7f8c8d;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.close-btn:hover {
  color: #e74c3c;
}

.modal-body {
  padding: 20px;
}

.hexagram-display {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 30px;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.symbol-large {
  color: #2c3e50;
  font-weight: bold;
}

.hexagram-lines-large {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.hexagram-line-large {
  font-family: 'Courier New', monospace;
  font-size: 2rem;
  color: #2c3e50;
  line-height: 1;
  font-weight: bold;
}

.info p {
  margin-bottom: 10px;
  line-height: 1.6;
}

.lines-section h3 {
  color: #34495e;
  margin-bottom: 15px;
  font-size: 1.3rem;
}

.lines {
  background: #f8f9fa;
  padding: 15px;
  border-radius: 8px;
}

.line {
  padding: 8px 0;
  border-bottom: 1px solid #dee2e6;
  font-size: 1rem;
}

.line:last-child {
  border-bottom: none;
}

@media (max-width: 768px) {
  .hexagrams-grid {
    grid-template-columns: 1fr;
  }
  
  .hexagram-display {
    flex-direction: column;
    text-align: center;
  }
  
  .symbol-large {
    font-size: 3rem;
  }
}
</style>
